import $ from "jquery";
import {url} from "../../util/config";
import getRole from "../role/getRole";
import {message} from "antd";
import login from "../login";

function getNewLogin() {
    return getUserBind()
}

export default function getUserBind() {

    getRole();

    let usersBind = [];
    let token = localStorage.getItem("token")
    $.ajax({

        async:false,
        headers: {
            'Accept': "application/json",
             'Authorization': token
        },
        url: url.api_url+`BindRole/GetUser`,
        type: 'GET',
        success: function (result) {
            let tmp = result.result;
            if(tmp=="Token過期請重新登錄"){
                login(localStorage.getItem("account"), localStorage.getItem("password"));
                usersBind = getNewLogin()
            }else {
                if(tmp == "该用户无权限查看！"){
                    return
                }
                usersBind = tmp.map(item=>{
                    return {
                        //       key:item.USER_WORK_NUM+"_"+item.USER_ORDER,  取消后缀user_order的绑定
                        key:item.USER_WORK_NUM,
                        name:item.USER_NAME,
                        userWorkNum:item.USER_WORK_NUM,
                        department:item.USER_DEPARTMENT,
                        createUser:item.CREATE_USER,
                        createTime:item.CREATE_DATE,
                        updateUser:item.UPDATE_USER,
                        updateDate:item.UPDATE_DATE
                        //      type:item.BIND_ROLE_TYPE,   取消
                        //      roleId:item.BIND_ROLE_ID,   取消
                        //      role:item.BIND_ROLE_NAME,   取消
                        //      sequence:item.USER_ORDER,   取消
                    }
                });
                sessionStorage.setItem("usersBind",JSON.stringify(usersBind))
            }
        },
        error:function (error) {
            message.error("请求异常")
        }
    })
    return usersBind;

}
